package com.haople.sso.jdbc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.haople.sso.core.util.DateTool;
import com.haople.sso.core.vo.Ticket;
import com.haople.sso.jdbc.dao.BaseDao;
import com.haople.sso.jdbc.dao.TicketDao;

@Repository("ticketDao")
public class TicketDaoImpl extends BaseDao implements TicketDao {
	private static final Logger logger=Logger.getLogger(TicketDaoImpl.class);
	
	@Value("${expire_day}")
	private int expire; //失效的天数
	
	/**
	 * 根据票据和用户的名称  获取最新的 ticket
	 * @param ticket
	 * @param userId
	 * @return
	 */
	public Ticket find(String userId) {
		//在没有失效的时间内 查询是否有ticket
		String sql="SELECT st.`ticket`,st.`user_id`,st.`user_name`,st.`create_date` FROM `sso_ticket` st WHERE st.`user_id`=?  ORDER BY st.`create_date` DESC LIMIT 0,1";
		try {
			return getJdbcTemplate().queryForObject(sql, new RowMapper<Ticket>(){

				public Ticket mapRow(ResultSet rs, int num) throws SQLException {
					Ticket ticket=new Ticket();
					ticket.setTicket(rs.getString("ticket"));
					ticket.setUserId(rs.getString("user_id"));
					ticket.setCreateDate(rs.getDate("create_date"));
					return ticket;
				}
				
			},userId);
		} catch (DataAccessException e) {
			logger.debug("------没有此票据-[ userId:"+userId+"  ]------", e);
		}
		return null;
	}
	
	/**
	 * 根据票据和用户的名称  获取最新的 ticket
	 * @param ticket
	 * @param userId
	 * @return
	 */
	public Ticket find(String ticket, String userId) {
		//在没有失效的时间内 查询是否有ticket
		String sql="SELECT st.`ticket`,st.`user_id`,st.`user_name`,st.`create_date` FROM `sso_ticket` st WHERE st.`ticket`=? AND st.`user_id`=?  AND st.`create_date` <= ? ORDER BY st.`create_date` DESC LIMIT 0,1";
		Date expireDate=new DateTool().addDay(expire);
		try {
			return getJdbcTemplate().queryForObject(sql, new RowMapper<Ticket>(){

				public Ticket mapRow(ResultSet rs, int num) throws SQLException {
					Ticket ticket=new Ticket();
					ticket.setTicket(rs.getString("ticket"));
					ticket.setUserId(rs.getString("user_id"));
					ticket.setCreateDate(rs.getDate("create_date"));
					return ticket;
				}
				
			}, ticket,userId,expireDate);
		} catch (DataAccessException e) {
			logger.debug("------没有此票据-[ ticket:"+ticket+",  userId:"+userId+"  ]------", e);
		}
		return null;
	}

	/**
	 * 保存票据
	 * 返回影响数
	 */
	public Integer save(String ticket, String userId,String userName) {
		try {
			String sql="INSERT INTO `sso_ticket`(`ticket`,`user_id`,`user_name`,`create_date`) VALUES(?,?,?,?)";
			return getJdbcTemplate().update(sql, ticket,userId,userName,new Date());
		} catch (DataAccessException e) {
			logger.error("---保存票据失败---[ ticket:"+ticket+"   userId:"+userId+"  userName:"+userName+"]---", e);
		}
		return 0;
	}

}
